﻿using RotovapPlatform.Domain.Business.Storage.Entities;
using RotovapPlatform.Domain.Business.Storage.Entities.Base;
using RotovapPlatform.Domain.Business.Storage.Entities.Bottles;
using RotovapPlatform.Domain.Business.Storage.Enums;
using RotovapPlatform.Domain.Services.Storages;
using static RotovapPlatform.Domain.Business.Machine.Enums.ExperimentConfigEnums;

namespace RotovapPlatform.Application.Services.Storages
{
	/// <summary>
	/// 存储应用服务
	/// 作为UI层和领域层之间的桥梁
	/// </summary>
	public class StorageAppService : IStorageAppService
	{
		private readonly IStorageDomainService _storageService;

		public StorageAppService(IStorageDomainService storageService)
		{
			_storageService = storageService;
		}

		/// <summary>
		/// 获取所有货架位置
		/// </summary>
		public async Task<List<StorageLocation>> GetAllShelfLocationsAsync()
		{
			return await _storageService.GetAllShelfLocationsAsync();
		}

		/// <summary>
		/// 获取指定货架位置
		/// </summary>
		public async Task<StorageLocation?> GetShelfLocationAsync(LocationCode locationCode, int position)
		{
			return await _storageService.GetShelfLocationAsync(locationCode, position);
		}

		/// <summary>
		/// 根据ID获取货架位置
		/// </summary>
		public async Task<StorageLocation?> GetShelfLocationByIdAsync(int id)
		{
			return await _storageService.GetShelfLocationByIdAsync(id);
		}

		/// <summary>
		/// 获取所有工作站位置
		/// </summary>
		public async Task<List<StorageLocation>> GetStationLocationsAsync(LocationCode stationType)
		{
			return await _storageService.GetStationLocationsAsync(stationType);
		}

		/// <summary>
		/// 获取可以放置指定物品类型的空闲货架位置
		/// </summary>
		public async Task<List<StorageLocation>> GetAvailableShelfLocationsForItemTypeAsync(ItemType itemType)
		{
			return await _storageService.GetAvailableShelfLocationsForItemTypeAsync(itemType);
		}

		/// <summary>
		/// 将物品放入指定货架位置
		/// </summary>
		public async Task<StorageLocation> PlaceItemAsync(int locationId, IStorageItem item)
		{
			return await _storageService.PlaceItemAsync(locationId, item);
		}

		/// <summary>
		/// 将物品放入指定货架位置（通过位置编号）
		/// </summary>
		public async Task<StorageLocation> PlaceItemByPositionAsync(LocationCode locationCode, int position, IStorageItem item)
		{
			// 先获取位置信息
			var location = await _storageService.GetShelfLocationAsync(locationCode, position);
			if (location == null)
			{
				throw new InvalidOperationException($"未找到位置: {locationCode}-{position}");
			}

			// 放入物品
			return await _storageService.PlaceItemAsync(location.Id, item);
		}

		/// <summary>
		/// 从指定货架位置取出物品
		/// </summary>
		public async Task<IStorageItem?> RemoveItemAsync(int locationId)
		{
			return await _storageService.RemoveItemAsync(locationId);
		}

		/// <summary>
		/// 从指定货架位置取出物品（通过位置编号）
		/// </summary>
		public async Task<IStorageItem?> RemoveItemByPositionAsync(LocationCode locationCode, int position)
		{
			// 先获取位置信息
			var location = await _storageService.GetShelfLocationAsync(locationCode, position);
			if (location == null)
			{
				throw new InvalidOperationException($"未找到位置: {locationCode}-{position}");
			}

			// 取出物品
			return await _storageService.RemoveItemAsync(location.Id);
		}

		/// <summary>
		/// 检查物品是否可以放入指定位置
		/// </summary>
		public async Task<bool> CanPlaceItemTypeAsync(int locationId, ItemType itemType)
		{
			return await _storageService.CanPlaceItemTypeAsync(locationId, itemType);
		}

		/// <summary>
		/// 检查物品是否可以放入指定位置（通过位置编号）
		/// </summary>
		public async Task<bool> CanPlaceItemTypeByPositionAsync(LocationCode locationCode, int position, ItemType itemType)
		{
			return await _storageService.CanPlaceItemTypeByPositionAsync(locationCode, position, itemType);
		}

		/// <summary>
		/// 获取货架位置的显示名称
		/// </summary>
		public string GetShelfDisplayName(int position)
		{
			return _storageService.GetShelfDisplayName(position);
		}

		/// <summary>
		/// 根据显示名称获取货架位置编号
		/// </summary>
		public int GetShelfPositionFromDisplayName(string displayName)
		{
			return _storageService.GetShelfPositionFromDisplayName(displayName);
		}

		/// <summary>
		/// 创建新的旋蒸瓶
		/// </summary>
		public RotovapBottle CreateRotovapBottle(bool is1000ml, FunctionType function, string materialName, bool isClean)
		{
			var bottle = new RotovapBottle()
			{
				Function = function,
				MaterialName = materialName,
				IsClean = isClean,
				StackInTime = DateTime.Now,
				Operator = "操作员", // 这里可以从当前登录用户获取
				BarCode = GenerateBarCode(is1000ml ? "RV1000" : "RV500")
			};

			// 设置瓶子类型
			if (is1000ml)
			{
				bottle.SetAs1000ml();
			}

			return bottle;
		}

		/// <summary>
		/// 创建新的离心瓶
		/// </summary>
		public CentrifugeBottle CreateCentrifugeBottle(FunctionType function, string materialName, double currentVolume)
		{
			var bottle = new CentrifugeBottle()
			{
				Function = function,
				MaterialName = materialName,
				CurrentVolume = currentVolume,
				StackInTime = DateTime.Now,
				Operator = "操作员", // 这里可以从当前登录用户获取
				BarCode = GenerateBarCode("CF500")
			};
			return bottle;
		}

		/// <summary>
		/// 创建新的取样瓶
		/// </summary>
		public SampleBottle CreateSampleBottle(FunctionType function, string materialName, double currentVolume)
		{
			var bottle = new SampleBottle()
			{
				Function = function,
				MaterialName = materialName,
				CurrentVolume = currentVolume,
				StackInTime = DateTime.Now,
				Operator = "操作员", // 这里可以从当前登录用户获取
				BarCode = GenerateBarCode("SP008")
			};
			return bottle;
		}

		/// <summary>
		/// 生成条形码
		/// </summary>
		private string GenerateBarCode(string prefix)
		{
			// 简单实现，实际应用中可能需要更复杂的逻辑
			return $"{prefix}-{DateTime.Now.ToString("yyyyMMddHHmmss")}";
		}
	}
}